package ru.samgtu.wst.plugin.fuzz;

import java.net.MalformedURLException;

import ru.samgtu.wst.model.HttpUrl;
import ru.samgtu.wst.model.Request;

import java.util.ArrayList;
import java.util.List;

public class Signature {

	private static Parameter[] NONE = new Parameter[0];

	private String _method;
	private HttpUrl _url;
	private String _contentType;
	private Parameter[] _parameters;

	/** Creates a new instance of Signature */
	public Signature(Request request) {
		_method = request.getMethod();
		_url = request.getURL();
		if (_url.getParameters() != null) {
			_url = _url.getParentUrl();
		}
		_contentType = request.getHeader("Content-Type");
		_parameters = Parameter.getParameters(request);
	}

	public Signature(String signature) throws MalformedURLException {
		String[] parts = signature.split(" ");
		_method = parts[0];
		_url = new HttpUrl(parts[1]);
		_contentType = parts[2].substring(1, parts[2].length() - 1);
		if (_contentType.equals("null")) {
			_contentType = null;
		}
		List parameters = new ArrayList();
		for (int i = 3; i < parts.length; i++) {
			int colon = parts[i].indexOf(":");
			String location = parts[i].substring(0, colon);
			int left = parts[i].indexOf('(', colon);
			String name = parts[i].substring(colon + 1, left);
			String type = parts[i].substring(left + 1, parts[i].length() - 1);
			Parameter param = new Parameter(location, name, type, "");
			parameters.add(param);
		}
		_parameters = (Parameter[]) parameters.toArray(Parameter.NO_PARAMS);
	}

	public String getMethod() {
		return _method;
	}

	public HttpUrl getUrl() {
		return _url;
	}

	public String getContentType() {
		return _contentType;
	}

	public Parameter[] getParameters() {
		return _parameters;
	}

	@Override
	public String toString() {
		StringBuffer buff = new StringBuffer();
		buff.append(_method).append(" ").append(_url).append(" ");
		buff.append("(").append(_contentType).append(")");
		for (int i = 0; i < _parameters.length; i++) {
			buff.append(" ").append(_parameters[i].getLocation()).append(":")
					.append(_parameters[i].getName());
			buff.append("(").append(_parameters[i].getType()).append(")");
		}
		return buff.toString();
	}

	@Override
	public boolean equals(Object obj) {
		if ((obj == null) || !(obj instanceof Signature)) {
			return false;
		}
		Signature that = (Signature) obj;
		if (!_method.equals(that._method)) {
			return false;
		}
		if (!_url.equals(that._url)) {
			return false;
		}
		if ((_contentType == null) && (that._contentType != null)) {
			return false;
		}
		if ((_contentType != null) && (that._contentType == null)) {
			return false;
		}
		if ((_contentType != null) && !_contentType.equals(that._contentType)) {
			return false;
		}
		if (_parameters.length != that._parameters.length) {
			return false;
		}
		for (int i = 0; i < _parameters.length; i++) {
			if (!_parameters[i].equals(that._parameters[i])) {
				return false;
			}
		}
		return true;
	}

}
